<script setup>
import { ref } from 'vue'
import {selectCourseSubListService,
    updateCourseSubListService,
    insertCourseSubListService,
    deleteCourseSubListService
} from '@/api/CourseManager.js'

const total = ref(0)
const params = ref({
    courseSubName:'',
  pageNum:1,
  pageSize:10
})

const handleClick = () => {
  console.log('click')
}
const courseSubList = ref([])
const courseSubVoRef = ref()
const courseSubVo = ref({
      id:'',
      courseId:'',
      courseName:'',
      courseSubName:'',
      courseSubTime: null,
      courseSubTimeStr:'',
})
const rules = {
    courseId:[{required:true,message:'请输入课程编号',trigger:'blur'}],
    courseSubName:[{required:true,message:'请输入课节名称',trigger:'blur'}],
    courseSubTime:[{required:true,message:'请输入课节时间',trigger:'blur'}],
}
const selectCourseSubList = async () =>{
    const res = await selectCourseSubListService(params.value)
    courseSubList.value = res.data.data
    total.value = res.data.total
}
selectCourseSubList()


const showDrawer = ref(false)
const editInsertBtn = () => {
    showDrawer.value = true;
}
const deleteRow = async (row) => {
    await deleteCourseSubListService(row)
    ElMessage.success('成功！')
    selectCourseSubList()
}
const editRow = (row) =>{
    showDrawer.value = true;
    courseSubVo.value = row
}
const confirmClick = async()=>{
    await courseSubVoRef.value.validate()
    if(courseSubVo.value.id){
        await updateCourseSubListService(courseSubVo.value)
    }else{
        await insertCourseSubListService(courseSubVo.value)
    }

    ElMessage.success('成功！')
    courseSubVo.value={}
    showDrawer.value =  false
    selectCourseSubList()
}

const search = ()=>{
    selectCourseSubList()
}
</script>

<template>
  <div>
    <div class="cus-header">
        <el-card>
            <div style="display: flex;justify-content: space-between;">
                <div class="search">
                <div style="margin-left: 20px;">
                    课节：
                    <el-input v-model="params.courseSubName" style="width: 240px" placeholder="请输入课节" />
                </div>
                <div style="margin-left: 20px;">
                    <el-button @click="search" style="width: 80px;" type="primary">搜索</el-button>
                </div>
            </div>
                <div class="insert">
                    <el-button style="width: 80px;" type="primary" @click="editInsertBtn">新增</el-button>
                </div>
            </div>
            
            
        </el-card>
    </div>
    <div class="cus-body">
        <el-card style="min-height: 650px;">
            <el-table :data="courseSubList" style="width: 100%">
                <el-table-column fixed prop="id" label="课节编号" width="150" />
                <el-table-column prop="courseName" label="课程名称" width="120" />
                <el-table-column prop="courseSubName" label="课节名称" width="120" />
                <el-table-column prop="courseSubTimeStr" label="课节时间" width="900" />
                <el-table-column fixed="right" label="操作" min-width="150">
                    <template #default="{row}">
                        <el-button link type="primary" size="small" @click="handleClick">
                            资源
                        </el-button>
                        <el-button link type="primary" size="small" @click="editRow(row)">
                            编辑
                        </el-button>
                        <el-button link type="primary" size="small" @click="deleteRow(row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>
    </div>
  </div>



  
    <!-- 新增或编辑 -->
    <el-drawer v-model="showDrawer" >
        <template #header>
            <h4>操作</h4>
        </template>
        <template #default>
            <div>
                <el-form
                :model="courseSubVo" 
                ref="courseSubVoRef"
                :rules="rules"
                label-width="auto">
                    <el-form-item label="课程编号" :label-position="left" prop="courseId">
                        <el-input v-model="courseSubVo.courseId" />
                    </el-form-item>
                    <el-form-item label="课节名称" :label-position="left" prop="courseSubName">
                        <el-input v-model="courseSubVo.courseSubName" />
                    </el-form-item>
                    <el-form-item label="课程时间" :label-position="left" prop="courseSubTime">
                        <el-date-picker
                        v-model="courseSubVo.courseSubTime"
                        type="datetime"
                        placeholder="选择日期时间">
                        </el-date-picker>
                    </el-form-item>
                </el-form>
            </div>
        </template>
        <template #footer>
        <div style="flex: auto">
            <el-button @click="showDrawer = false">取消</el-button>
            <el-button type="primary" @click="confirmClick">提交</el-button>
        </div>
        </template> 
    </el-drawer>
</template>


<style lang="scss" scoped>
    .flex-row{
        display: flex;
        flex-direction: row;
    }
    .cus-header{
        .search{
            display: flex;
            flex-direction: row;
        }
    }
    .cus-body{
        margin-top: 15px;
    }
</style>